﻿using System; 
using System.Collections.Generic;
using System.Text;
using SCG.SolarVengeanceEngine;

namespace SCG.SolarVengeance.Scenarios
{
    public class ArmsRace : Scenario
    {
        public ArmsRace() : base()
        {
            vc = CreateParameter("VC Resources", 2000, 1000, 10000);
        }

        public override void BuildScenario(int numPlayers, int capitalValues, int startingResources)
        {          
            //Provide Freighters
            AddedStarShipTypes.Add(StarShipType.Freighter);

            //Create the Vornal Hive
            CreateStarSystem(Random(40, 60), Random(40, 60), 20, Random(800, 1000), Random(1, 30), Random(4000, 6000)).Name = "Vornal Hive";
            vornal = Game.StarSystems[0];

            //Create the rich central cluster
            CreateStarCluster(numPlayers * 3 + 10, 50, 50, 50, 50, 20, 1);
            for (int i = 1; i < Game.StarSystems.Count; i++)
            {
                StarSystem ss = Game.StarSystems[i];
                ss.Resources = Random(20, 500);
                ss.Value = Random(10, 30);
            }

            //Create StarSystems on outside edge - assign capitals
            int edge = 10 + numPlayers * 3;
            for (int n = 0; n < edge; n++)
            {
                int x;
                int y;
                do
                {
                    x = Random(1, 98);
                    y = Random(1, 98);
                }
                while (!EdgeLocationOK(x, y));
                CreateStarSystem(x, y, RandLowWeight(1, 20), RandLowWeight(1, 30), RandLowWeight(1, 30), RandLowWeight(1, 30));
                if (n < Game.Players.Count)
                    Game.Players[n].Capital = Game.StarSystems[Game.StarSystems.Count - 1];
            }
        }

        //Perform auto-shielding
        public override void ProcessImpulse(int impulse)
        {
            base.ProcessImpulse(impulse);     
     
            //Auto-shielding technology
            AutoShielding(vornal);
            try
            {
                foreach (Player p in Game.Players)
                    if (!p.Eliminated)
                        AutoShielding(p.Capital);
            }
            catch
            {
                //in case a player has dropped, avoid exception
            }
        }

        //Victory conditions
        public override bool VictoryReached()
        {
            //Hive captured?
            if (vornal.Owner != null)
            {
                vornal.Owner.Victor = true;
                return true;
            }

            //Player has 5000 resources?
            foreach(Player p in Game.Players)
                if (!p.Eliminated)
                    if (p.Capital.Resources >= vc.Value)
                    {
                        p.Victor = true;
                        return true;
                    }

            //allow default VC
            return base.VictoryReached();
        }

        //Description
        public override string Description
        {
            get
            {
                return "The K'ptian Sector was the location of the Vornal Hive, a race of mysterious, advanced aliens.  The Hive has developed a " +
                    "Doomsday Weapon that would shift the balance of power to the Star Lord who can afford to purchase it.  The Hive is offering the " +
                    "device for 2,000 Resources.  The Star Lord that can collect 2,000 Resources at their Capital will claim the Weapon, and Victory.  " +
                    "An alternate Victory Condition is destruction of the Hive's heavilly fortified homeworld.  Star Lords begin with Freighters so " +
                    "they can collect Resources from the rich central cluster.  Player Capitals possess advanced auto-Shielding technology.";
            }
        }

        //private variables
        private StarSystem vornal;
        private ScenarioParameter vc;
        private Random rnd = new Random();

        //auto-shield routine
        private void AutoShielding(StarSystem system)
        {
            if (system.Shields < 100)
                DynamicEvents.AdjustStarSystemValues(system, null, null, system.Shields + 1, null);
        }

        //Is this an appropriate location for an edge StarSystem?
        private bool EdgeLocationOK(int x, int y)
        {
            double dist = Distance(x, y, 50, 50);
            if (dist < 20 || dist > 40)
                return false;
            
            foreach (Player p in Game.Players)
                if (p.Capital != null)
                    if (Distance(x, y, p.Capital.X, p.Capital.Y) < 12)
                        return false;
            return true;
        }

        //Create a random number with a weighting to lower numbers
        private int RandLowWeight(int from, int to)
        {
            return (int)(Random(from, to) * rnd.NextDouble());
        }
    }
}
